package com.lsyw.intergrate.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsyw.intergrate.common.Result;
import org.springframework.util.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import com.lsyw.intergrate.service.MesPointService;
import com.lsyw.intergrate.entity.MesPoint;

import java.util.*;

import io.swagger.annotations.Api;
import lombok.Data;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2024-02-21
 */
@Api(tags = "mes监控点")
@RestController
@RequestMapping("/mes-point")
public class MesPointController {


    @Autowired
    private MesPointService mesPointService;


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    public Result getById(String id) {
        return Result.successResult(mesPointService.getById(id));
    }

    /**
     * 通过id查询
     */
    @PostMapping("/test")
    public Result test(@RequestBody List<JSONObject> list) {
        list.forEach(e->{
            MesPoint mesPoint=new MesPoint();
            mesPoint.setDelFlag(0);
            mesPoint.setGmtCreate(new Date());
            mesPoint.setMassifId("1729058745132441602");
            mesPoint.setName(e.get("n").toString());
            mesPointService.save(mesPoint);
        });
        return Result.successResult(null);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody MesPoint mesPoint) {
        mesPoint.setDelFlag(0);
        mesPoint.setGmtCreate(new Date());
        mesPointService.save(mesPoint);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String id) {
        mesPointService.removeById(Long.parseLong(id));
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody MesPoint mesPoint) {
        mesPointService.updateById(mesPoint);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody MesPointReqVo mesPoint) {
        final LambdaQueryWrapper<MesPoint> lambda = new QueryWrapper<MesPoint>().lambda();
        this.buildCondition(lambda, mesPoint);
        return Result.successResult(mesPointService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<MesPoint> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<MesPoint> lambda = new QueryWrapper<MesPoint>().lambda();
        lambda.eq(MesPoint::getDelFlag,0);
        //TODO 自己的查询逻辑
        return Result.successResult(this.mesPointService.page(page, lambda));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<MesPoint> lambda, MesPointReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(MesPoint::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(MesPoint::getName, param.getName());
        }
        if (!StringUtils.isEmpty(param.getMassifId())) {
            lambda.eq(MesPoint::getMassifId, param.getMassifId());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(MesPoint::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(MesPoint::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(MesPoint::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(MesPoint::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(MesPoint::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(MesPoint::getGmtModified, param.getGmtModifiedList().get(1));
        }
            lambda.eq(MesPoint::getDelFlag,0);
        lambda.orderBy(true, false, MesPoint::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class MesPointReqVo extends MesPoint {
        private List<String> gmtCreateList; // 起止
        private List<String> gmtModifiedList; // 起止
    }


}
